﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Valentia.Kinection.DataTypes;

namespace Valentia.Kinection.Remoting.Proxy
{
    /// <summary>
    /// This class is used for communication among Remoting Server and Remoting Client
    /// </summary>
    public class RemotingProxy : MarshalByRefObject, IRemotingServerObject
    {
        #region Declarations

        /// <summary>
        /// This event will be fired when server needs to send information of devices connection to client.
        /// </summary>
        public event DeviceConnectivityInfo KinectDeviceInfoReceived;

        /// <summary>
        /// This event will be fired when server needs to send information of client connected to service.
        /// </summary>
        public event ClientConnectivityInfo ServiceClientInfoReceived;

        /// <summary>
        /// This event will be fired when server needs to send information of service client and device connectivity status.
        /// </summary>
        public event ConnectionStatus ConnectionStatusInfoReceived;

        /// <summary>
        /// This event will be fired when server needs to show notification on service manager utility.
        /// </summary>
        public event EventHandler<NotificationReceivedArgs> ShowNotificationReceived; 

        #endregion

        #region Public Methods

        /// <summary>
        /// This method will be used to send command from client to server
        /// </summary>
        /// <param name="command">Command</param>
        public String CommandFromClient(ClientCommands command, String loginName, String password)
        {
            return null;
        }

        /// <summary>
        /// This method will be used to determine client and server connection is alive
        /// </summary>
        /// <returns>Server will return true</returns>
        public bool IsConnectionAlive()
        {
            return true;
        }

        #endregion

        #region Over ridden Methods

        /// <summary>
        ///  Retrieves the current lifetime service object that controls the lifetime
        ///  policy for this instance.
        /// </summary>
        /// <returns> An object of type System.Runtime.Remoting.Lifetime.ILease used to control
        ///   the lifetime policy for this instance.</returns>
        public override object InitializeLifetimeService()
        {
            //Returning null holds the object alive until it is explicitly destroyed
            return null;
        }
       
        #endregion

        #region Server Event Handling Helper

        /// <summary>
        /// Handles the device connection event and passes information to remoting client
        /// </summary>
        /// <param name="deviceConnectionInfo">Device connectivity information</param>
        /// <param name="errorMessage">Error message</param>
        public void HandleDeviceConnectionInfoEvent(List<KinectDeviceInfo> deviceConnectionInfo,  String errorMessage)
        {
            if (KinectDeviceInfoReceived != null)
            {
                KinectDeviceInfoReceived(deviceConnectionInfo, errorMessage);
            }
        }

        /// <summary>
        /// Handles the service clients connection event and passes information to remoting client
        /// </summary>
        /// <param name="clientConnectionInfo">Service client connectivity information</param>
        /// <param name="errorMessage">Error message</param>
        public void HandleClientConnectionInfoEvent(List<ServiceClientInfo> clientConnectionInfo, String errorMessage)
        {
            if (ServiceClientInfoReceived != null)
            {
                ServiceClientInfoReceived(clientConnectionInfo, errorMessage);
            }
        }

        /// <summary>
        /// Handles the connectivity status event and passes information to remoting client
        /// </summary>
        /// <param name="connectivityStatus">Device/Service client connectivity status</param>
        /// <param name="connectedDeviceCount">Connected devices count</param>
        /// <param name="connectedClientCount">Connected service clients count</param>
        public void HandleConnectionStatusEvent(ConnectivityStatus connectivityStatus, int connectedDeviceCount
            , int connectedClientCount, String errorMessage)
        {
            if (ConnectionStatusInfoReceived != null)
            {
                ConnectionStatusInfoReceived(connectivityStatus, connectedDeviceCount, connectedClientCount, errorMessage);
            }
        }

        /// <summary>
        /// Handles the notification event and passes information to client
        /// </summary>
        /// <param name="obj">Sender info information</param>
        /// <param name="notificationArgs">Notification information</param>
        public void HandleNotificationEvent(object obj, NotificationReceivedArgs notificationArgs)
        {
            if (ShowNotificationReceived != null)
            {
                ShowNotificationReceived(null, notificationArgs);
            }
        }

        #endregion
    }
}
